<template>
  <a-card :bordered="false">
    <a-tabs :default-active-key="0" @change="callback">
      <a-tab-pane :key="index" :tab="item.name" v-for='(item,index) in appList'/>
    </a-tabs>
    <v-table
      :columns="tableObj.columns"
      :data="loadData"
      :showPagination="false"
      ref="table"
      :auto-search='false'
    >
      <template v-slot:buttons>
        <a-button @click="addData" icon="plus" type="primary">新建</a-button>
      </template>
      <span slot="type" slot-scope="{ row,text }">
        <span v-show="String(text) === '0'">目录</span>
        <span v-show="String(text) === '1'">菜单</span>
        <span v-show="String(text) === '2'">按钮</span>
      </span>
      <template slot="icon" slot-scope="{ text }">
        <div v-if="text != ''">
          <a-icon :type="text"/>
        </div>
      </template>
      <span slot="action" slot-scope="{ row }">
        <a href="javascript:;" @click="edit(row)">编辑</a>
        <a-divider type="vertical" />
        <a-popconfirm title="是否删除权限" @confirm="deleteItem(row)" okText="是" cancelText="否">
          <a-icon slot="icon" type="question-circle-o" style="color: red" />
          <a href="javascript:;" style="color: red">删除</a>
        </a-popconfirm>
      </span>
    </v-table>
    <perm-form
      :code='code'
      ref="modalForm"
      @ok="handleOk"/>
  </a-card>
</template>

<script>
import { fetchList, delObj } from '@/api/sys/perm'
import { list } from '@/api/sys/app'
import permForm from './permForm'
import { tableObj } from './template'
import { TableMixin } from '@/mixins/TableMixin'
export default {
  name: 'Menu',
  mixins: [TableMixin],
  components: {
    permForm
  },
  data () {
    return {
      tableObj,
      loading: false,
      tableData: [],
      loadData: () => {
        return fetchList({
          code: this.code
        }).then(res => {
          return res.data
        })
      },
      appList: [],
      code: ''
    }
  },
  methods: {
    callback(key){
      this.code = this.appList[key].code
      this.queryPage()
    },
    deleteItem (record) {
      delObj(record.id).then(_ => {
        this.$message.info('删除成功')
        this.queryPage()
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    edit (row) {
      this.$refs.modalForm.init(row, 'edit')
    },
    handleOk () {
      this.queryPage()
    }
  },
  created() {
    list().then(res => {
      this.appList = res.data
      this.code = this.appList[0].code
      this.queryPage()
    }).catch(err => {
      this.$message.error(err.msg)
    })
  }
}
</script>

<style scoped>

</style>
